﻿using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebServiceSmart;

namespace WebServiceShopping.Thrift
{
    public class Helper
    {
        /// <summary>
        /// 获取产品4s价格(拾车道)
        /// </summary>
        /// <param name="content"></param>
        /// <param name="productsJson"></param>
        /// <returns></returns>
        public static string GetShichedao(string content, JArray productsJson, ILog loginfo)
        {

            var products = string.Empty;
            try
            {
                string partNumber = string.Empty;
                string price4S = string.Empty;
                JArray contentArr = JsonConvert.DeserializeObject<JArray>(content);

                //拾车道账号license
                var tokenData = new
                {
                    license = "910a99e4dc2bd2b7eff9e460945ea2c5",
                };

                //获取Token
                var poststr = JsonConvert.SerializeObject(tokenData);
                var result = SmsSendInterface.postJson("https://openapi.51qcj.com/api/v1.0/secret/token", poststr);
                JObject resultJson = JObject.Parse(result);

                if (resultJson["code"] != null && resultJson["code"].ToString().Equals("200"))
                {
                    var token = resultJson["content"]["access"].ToString();
                    var vin = contentArr[2].ToString();

                    //车架号
                    var vinData = new
                    {
                        vin = vin
                    };

                    //获取车辆目录编号，车厂Id
                    poststr = JsonConvert.SerializeObject(vinData);
                    result = SmsSendInterface.postJson("https://openapi.51qcj.com/api/v1.0/epc/vinexactextend", poststr, token);
                    resultJson = JObject.Parse(result);

                    if (resultJson["code"] != null && resultJson["code"].ToString().Equals("200"))
                    {
                        if (resultJson["content"].Count() > 0)
                        {
                            var manufacturerId = resultJson["content"][0]["translate"]["manufacturerId"].ToString();
                            var seriesId = resultJson["content"][0]["translate"]["seriesId"].ToString();

                            //查询指定产品
                            if (productsJson != null)
                            {
                                JArray obj = new JArray();
                                foreach (JToken item in productsJson)
                                {
                                    var translateData = new
                                    {
                                        vin = vin,
                                        facNumber = manufacturerId,
                                        seriesNumber = seriesId,
                                        partGroupName = item["name"].ToString()
                                    };

                                    poststr = JsonConvert.SerializeObject(translateData);
                                    result = SmsSendInterface.postJson("https://openapi.51qcj.com/api/v1.0/epc/translate", poststr, token);
                                    resultJson = JObject.Parse(result);

                                    if (resultJson["code"] != null && resultJson["code"].ToString().Equals("200"))
                                    {
                                        partNumber = resultJson["content"][0]["partNumber"].ToString();
                                        price4S = resultJson["content"][0]["price4S"].ToString();
                                    }

                                    //重新定义products
                                    var itemJobject = JObject.Parse(item.ToString());
                                    JProperty partNumberProp = new JProperty("partNumber", partNumber);
                                    JProperty price4SProp = new JProperty("price4S", price4S);
                                    itemJobject.Add(partNumberProp);
                                    itemJobject.Add(price4SProp);
                                    obj.Add(itemJobject);
                                }

                                products = obj.ToString();
                            }
                        }
                    }
                }
            }
            catch(Exception e)
            {
                loginfo.Info("获取4S价格错误");
            }
            return products;
        }
    }
}